﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
namespace BLL
{
    public class MessageBLL
    {
        DAL.MessageDAL messagedal = new DAL.MessageDAL();
        /// <summary>
        /// 添加/修改一条信息
        /// </summary>
        public bool addMessage(Albon.Model.Message message)
        {
            return messagedal.addMessage(message);
        }

        /// <summary>
        /// 根据ID获取一条信息的详细内容
        /// </summary>
        public DataTable getMessageByID(string id)
        {
            return messagedal.getMessage("ID="+id);
        }
        /// <summary>
        /// 获取完整的信息显示（包括之前相关的所有信息）
        /// </summary>
        public string getMessage(string id)
        {
            DataTable dt=getMessageByID(id);
            StringBuilder mes = new StringBuilder();
            mes.Append("<table width='100%'>");
            if (dt.Rows[0]["refID"].ToString() != "0")
            {

                DataTable dtRef = getMessageByID(dt.Rows[0]["refID"].ToString());
                appendMessage(mes, dtRef.Rows[0],id);
                DataTable dtRe = messagedal.getMessage("refID=" + dt.Rows[0]["refID"].ToString());
                foreach (DataRow dr in dtRe.Rows)
                {
                    appendMessage(mes, dr,id);
                }
            }
            else {
                DataTable dtRef =messagedal.getMessage("refID=" + dt.Rows[0]["ID"].ToString());
                appendMessage(mes, dt.Rows[0],id);
                foreach (DataRow dr in dtRef.Rows)
                {
                    appendMessage(mes, dr, id);
                }
                
            }
            mes.Append("</table>");
            return mes.ToString();
        }
        protected StringBuilder appendMessage(StringBuilder sb, DataRow dr, string id)
        {
            sb.Append(@"<tr class='" + (id == dr["ID"].ToString() ? "outstand" : "") + "'><td class='td_left'>"
                + (dr["refID"].ToString() == "0" ? "Ref:" : "RE:")
                + "<img src='" + (bool.Parse(dr["isMarked"].ToString()) ? "/images/mark.gif" : "/images/unmark.gif") + "' alt='' />"
                + "</td><td>From:" + dr["sender"].ToString() + "</td><td>"
                + (dr["refID"].ToString() == "0" ? dr["title"].ToString() : "")
                + "</td><td>"
                   + (DateTime.Parse(dr["createDate"].ToString())).ToString("MM/dd/yyyy  hh:mm:dd") + "</td></tr>");
            sb.Append("<tr class='" + (id == dr["ID"].ToString() ? "outstand" : "") + "'><td></td><td colspan='3'>" + dr["content"].ToString() + "</td></tr>");
            //当收件人是当前登录用户，该信息已被显示，并且该订单已经被Process的，将当前邮件设为已读
            //如果是与订单无关的信息，则要将订单ID设为0
            OrderBLL orderbll = new OrderBLL();
            bool mark = true;
            if (dr["orderid"].ToString() != "0")
            {
                mark = mark && orderbll.isProcessed(dr["orderID"].ToString());
            }
            mark = mark && (System.Web.HttpContext.Current.User.Identity.Name == dr["receiver"].ToString());
            if (mark||dr["orderid"].ToString()=="0")
            {
                messagedal.readMessage(dr["ID"].ToString());
            }
            return sb;
        }
        /// <summary>
        /// 回复信息
        /// </summary>
        public bool replyMessage(Albon.Model.Message message)
        {
            return messagedal.replyMessage(message);
        }

        /// <summary>
        /// 判断是当前订单是否存在未读邮件（自己发送的，未被对方阅读）
        /// </summary>
        /// <param name="orderid"></param>
        public bool hasUnRead(string orderid, string username)
        {
            DataTable dt = messagedal.getMessage("orderID="+orderid+" and sender='"+username+"' and isRead=0");
            return dt.Rows.Count > 0;
        }
        /// <summary>
        /// 判断当前订单是否有新邮件（对方发送的，自己未阅读）
        /// </summary>
        public bool hasNewMessage(string orderid, string username)
        {
            DataTable dt = messagedal.getMessage("orderID=" + orderid + " and receiver='" + username + "' and isRead=0");
            return dt.Rows.Count > 0;
        }
    }
}
